//
//  FrankSlider.m
//  HH
//
//  Created by yingqi on 2018/8/14.
//  Copyright © 2018年 yingqi. All rights reserved.
//

#import "FrankSlider.h"

@interface FrankSlider()

@property (nonatomic, strong) UILabel *valueLabel;

@end

@implementation FrankSlider

- (UILabel *)valueLabel{
    if (!_valueLabel) {
        _valueLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _valueLabel.textColor = [UIColor blackColor];
        _valueLabel.textAlignment = NSTextAlignmentCenter;
        _valueLabel.font = [UIFont systemFontOfSize:11];
    }
    return _valueLabel;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setup];
        
        UIImage *image = [self OriginImage:[UIImage imageNamed:@"thub"] scaleToSize:CGSizeMake(40, 18)];
        [self setThumbImage:image forState:UIControlStateNormal];
    }
    return self;
}

- (void)setup{
    [self addSubview:self.valueLabel];
}

- (UIImage *)OriginImage:(UIImage *)image scaleToSize:(CGSize)size
{
    UIGraphicsBeginImageContext(size);
    [image drawInRect:CGRectMake(0,0, size.width, size.height)];
    UIImage *scaleImage=UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return scaleImage;
}

- (CGRect)thumbRectForBounds:(CGRect)bounds trackRect:(CGRect)rect value:(float)value{
   
    CGRect newRect = [super thumbRectForBounds:bounds trackRect:rect value:value];

    if (self.valueLabel) {
        self.valueLabel.frame = newRect;
        self.valueLabel.text = [NSString stringWithFormat:@"%d/%d",(int)value,(int)self.maximumValue];
        [self bringSubviewToFront:self.valueLabel];
    }
    return newRect;
}

@end
